package com.group.Controller;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.Enity.Building;
import com.group.Enity.CarCharge;
import com.group.Enity.Owner;
import com.group.Service.OwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("owner")
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    @ResponseBody
    @RequestMapping("queryOwnerAll")
    public JSONObject queryOwnerAll(Integer page,Integer limit,String identity,String tel)
    {

        PageHelper.startPage(page,limit);
        List<Owner> list = ownerService.getOwners(identity,tel);
        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list_new);
        return json;
    }

    @ResponseBody
    @RequestMapping("add")
    public String add(@RequestBody JSONObject jsonObject)
    {
        //System.out.println(jsonObject);
        int houseId = jsonObject.getInt("houseId");
        String sex = jsonObject.getStr("sex");
        String identity = jsonObject.getStr("identity");
        String tel = jsonObject.getStr("tel");
        String remarks = jsonObject.getStr("remarks");
        String username = jsonObject.getStr("username");

        Owner owner = new Owner();
        owner.setHouse_id(houseId);
        owner.setSex(sex);
        owner.setIdentity(identity);
        owner.setTel(tel);
        owner.setRemarks(remarks);
        owner.setUsername(username);

        ownerService.add(owner);

        return "";

    }


    @ResponseBody
    @RequestMapping("edit")
    public String edit(@RequestBody JSONObject jsonObject)
    {
        System.out.println(jsonObject);
        int houseId = jsonObject.getInt("houseId");
        String sex = jsonObject.getStr("sex");
        String identity = jsonObject.getStr("identity");
        String tel = jsonObject.getStr("tel");
        String remarks = jsonObject.getStr("remarks");
        String username = jsonObject.getStr("username");
        int id = jsonObject.getInt("id");

        Owner owner = new Owner();
        owner.setHouse_id(houseId);
        owner.setSex(sex);
        owner.setIdentity(identity);
        owner.setTel(tel);
        owner.setId(id);
        owner.setRemarks(remarks);
        owner.setUsername(username);
        ownerService.update(owner);

        return "";

    }

    @ResponseBody
    @RequestMapping("queryAll")
    public List<Owner> queryAll(){
        return ownerService.getOwners(null,null);
    }

    @ResponseBody
    @RequestMapping("deleteByIds")
    public String deleteByIds(String ids){
        //System.out.println(ids);
        String[] lst = ids.split(",");
        for (int index = 0;index < lst.length;index++)
        {
            int id = Integer.valueOf(lst[index]);
            ownerService.delete(id);
            //houseService.delete(id);
        }
        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json.toString();
    }

}
